<?php /*a:1:{s:61:"D:\web\www\tp5.1\public/../app/admin\view\database\index.html";i:1517450010;}*/ ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>表格</title>
    <link rel="stylesheet" href="/static/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/static/frame/static/css/style.css">
    <link rel="icon" href="/static/frame/static/image/code.png">
</head>
<body class="body">


<div class="layui-form">
    <a id="export" class="layui-btn site-demo-active layui-btn-sm" href="javascript:;" autocomplete="off">立即备份</a>

    <a id="optimize" href="<?php echo url('admin/Database/optimize'); ?>" class="layui-btn site-demo-active layui-btn-sm">优化表</a>
    <a id="repair" href="<?php echo url('admin/Database/repair'); ?>" class="layui-btn site-demo-active layui-btn-sm">修复表</a>
    <a  href="<?php echo url('admin/Database/importlist'); ?>" class="layui-btn site-demo-active layui-btn-sm">还原数据库</a>
    <form id="export-form" method="post" action="<?php echo url('admin/Database/export'); ?>">
    <table class="layui-table">


      <thead>
        <tr>
        <th width="48"><input class="check-all" checked="chedked" type="checkbox" value="" lay-skin="primary" lay-filter="allChoose"></th>
          <th>表名</th>
          <th>数据量</th>
          <th>数据大小</th>
          <th>创建时间</th>
          <th>备份状态</th>
          <th>操作</th>
        </tr> 
      </thead>
    


  <tbody>
    <?php if(is_array($list) || $list instanceof \think\Collection || $list instanceof \think\Paginator): if( count($list)==0 ) : echo "" ;else: foreach($list as $key=>$table): ?>   
       <tr>
          <td>
              <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="<?php echo htmlentities($table['name']); ?>" lay-skin="primary">
          </td>
          <td><?php echo htmlentities($table['name']); ?></td>
          <td><?php echo htmlentities($table['rows']); ?></td>
          <td><?php echo htmlentities(format_bytes($table['data_length'])); ?></td>
          <td><?php echo htmlentities($table['create_time']); ?></td>
          <td class="info">未备份</td>
          <td>
              <a class="optimize" href="<?php echo url('admin/Database/optimize',['tables'=>$table['name']]); ?>">优化表</a>&nbsp;
              <a class="repair" href="<?php echo url('admin/Database/repair',['tables'=>$table['name']]); ?>">修复表</a>
          </td>
        </tr>
    <?php endforeach; endif; else: echo "" ;endif; ?>
      </tbody>


    </table>
  </form>
  </div>



<script type="text/javascript" src="/static/frame/layui/layui.js"></script>

<script>
    layui.use(['form','layer'],function(){
     var $ = layui.jquery
      ,form = layui.form
      ,layer = layui.layer;
      //备份表方法
      $("#export").click(function(){
          $(this).html("正在发送备份请求...");
          $.post(
            $("#export-form").attr("action"),
            $("#export-form").serialize(), 
            function(data){
               
              if(data.code==1){
                $("#export").html( "开始备份，请不要关闭本页面！");
                backup(data.data.tab);
                window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
              }else{
                 layer.tips(data.msg, "#export", {
                  tips: [1, '#3595CC'],
                  time: 4000
                });
                $("#export").html("立即备份");
              }
              
            }, "json");
            return false;  
      }); 
      //递归备份表
      function backup(tab,status){
        status && showmsg(tab.id, "开始备份...(0%)");
        $.get( $("#export-form").attr("action"), tab, function(data){
          // console.log(data)
                if(data.code==1){
                  showmsg(tab, data.msg);

                  if(!$.isPlainObject(data.data.tab)){
                    $("#export").html("备份完成");
                    window.onbeforeunload = function(){ return null }
                    return;
                  } 

                  backup(data.data.tab, tab.id != data.data.tab.id); //递归
                } else {
                  $("#export").html("立即备份");
                }
            }, "json");

      }
      //修改备份状态
      function showmsg(tab, msg){
         $("table tbody tr").eq(tab.id).find(".info").html(msg)
      }
      //全选
      form.on('checkbox(allChoose)', function(data){
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
        child.each(function(index, item){
          item.checked = data.elem.checked;
        });
        form.render('checkbox');
      });

     //优化表,修复表 全部
      $("#optimize,#repair").click(function(){
        that = $(this);
           $.post(this.href, $("#export-form").serialize(), function(data){
           
            layer.tips(data.msg, that, {
              tips: [1, '#3595CC'],
              time: 4000
            });
    
            }, "json");
            return false;    
      });

      //优化表,修复表 单个
      $('.repair,.optimize').on('click',function(){
        info = $(this).parents('tr').find('td.info');
        $.get(this.href,function(data){
          info.html(data.msg)
        },'json');
        return false;
      });


    });

  </script>


</body>
</html>